/*
 * Copyright 2010 Joshua Smith and Nelson Llewellyn
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package norse.view.freyja;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import norse.view.NorseView;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import org.jfxtras.scene.layout.MigLayout;
import javafx.scene.shape.Rectangle;
import org.jfxtras.scene.layout.MigNodeLayoutInfo;
import norse.client.NorseClient;
import javafx.scene.Group;
import norse.model.NorseJfxScan;
import norse.view.NorseViewHost;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;


public class Freyja extends NorseView {

    public var controller: NorseClient;
    public var viewHosts: NorseViewHost[] on replace {
        var timeline = Timeline{};
        var firstReveal = 0s;
        var revealTime = firstReveal;
        for (host in viewHosts) {
                revealTime += 2s;
                insert KeyFrame {
                            time: revealTime
                            action: function () {
                                (host as FreyjaHost).reveal();
                            }

                        } into timeline.keyFrames;

        }
        timeline.play();

    }

    
    public var view:NorseView = this;

    var detailsPane: DetailsPane;

    public override function show(): Void {
        var stage = Stage {
                    title: "NoRSe"
                    resizable: false
                    scene: Scene {
                        content: MigLayout {
                            constraints: "fill"
                            content: [
                                Group { // left side of UI

                                    layoutInfo: MigNodeLayoutInfo {
                                        constraints: "WEST"
                                    }
                                    content: [
                                        LogoPane{},
                                        detailsPane = DetailsPane{
                                                translateY: 100
                                                details: bind selectedHost.getDetails()
                                                },
                                        TerminalPane {
                                                translateY: 350
                                                }
                                    ]
                                },
                                Group {
                                    layoutInfo: MigNodeLayoutInfo {
                                        constraints: "CENTER"
                                    }
                                    content: bind [
                                        Rectangle {
                                            width: 800
                                            height: 600
                                            fill: Color.BLACK
                                            blocksMouse: true
                                        },
                                        viewHosts
                                    ] },
                                Button {
                                    text: "Scan"
                                    layoutInfo: MigNodeLayoutInfo {
                                        constraints: "SOUTH"
                                    }
                                    onMouseClicked: function (event: MouseEvent) {
                                        println("scan!!");
                                        controller.model = controller.scan();
                                    }
                                }
                            ]
                        }
                    }
                }
    }

    public override function modelToView(model: NorseJfxScan): NorseViewHost[] {
        for (host in model.hosts where host.up) {
            FreyjaHost.modelToView(host, this);
        }
    }


}


